/**
 * Created by duwei on 2018/11/17.
 */
var PlayerInfoLocationWindow = Window.extend({
    _ccsFileName: res.PlayerInfoLocationWindow_json,
    position2playerArrIdx: {},
    /**
     * 初始化
     */
    initWindow: function (isMain, room) {
        var self = this;
        self.isMain = isMain;
        self.room = room;
        self.isUpdate = true;
        self._nodeList["_btn_tongyi"].setVisible(!self.isMain);
        self._nodeList["_btn_jujue"].setVisible(!self.isMain);
        self._nodeList["_btn_MainTongyi"].setVisible(self.isMain);
        self._nodeList["_btn_MainJujue"].setVisible(self.isMain);
        if (self.isMain) {
            TouchUtil.setOnclickListener(self._nodeList["_btn_MainTongyi"], function () {
                showLoading('等待加入房间...');
                network.send(3002, {
                    room_id: gameData.roomId,
                    ForceJoin: true
                });
                self.removeFromParent(true);
            });

            TouchUtil.setOnclickListener(self._nodeList["_btn_MainJujue"], function () {
                self.removeFromParent(true);
            });
        } else {
            TouchUtil.setOnclickListener(self._nodeList["_btn_tongyi"], function () {
                network.send(3004, {room_id: gameData.roomId});
                self.removeFromParent(true);
            });

            TouchUtil.setOnclickListener(self._nodeList["_btn_jujue"], function () {
                network.send(3003, {room_id: gameData.roomId});
                self.removeFromParent(true);
            });
        }
        TouchUtil.setOnclickListener(self._nodeList["_btn_back"], function () {
            self.removeFromParent(true);
        });
        TouchUtil.setOnclickListener(self._nodeList["_tabBtnsNode"]["btn_tab0"], function () {
            self.refreshTabBtn(0);
        });
        TouchUtil.setOnclickListener(self._nodeList["_tabBtnsNode"]["btn_tab1"], function () {
            self.refreshTabBtn(1);
        });
        self._nodeList['_roomId'].setString('房间号：' + gameData.roomId);
        self._nodeList['_distance'].setString('限制距离：' + gameData.distance + '米');
        self.refreshTabBtn(0);
        self.updataJuli();
        if (!gameData.isReplay) {
            self.scheduleUpdate();
        }
    },
    update: function (dt) {
        var self = this;
        if (!this._time)
            this._time = 0;
        this._time += dt * 1000;
        if (this._time >= 18000 && self.isUpdate && self.isVisible()) {
            this._time -= 18000;
        }
        for (var k = 0; k < gameData.players.length; k++) {
            var playerInfo = gameData.players[k];
            var loccation = playerInfo['loc'] || playerInfo['Location'];
            var locUpdTime = playerInfo['locUpdTime'];
            playerInfo['locUpdTimeStr'] = locUpdTime ? timeStr(locUpdTime) : "";

            var locationInfoNode = self._nodeList["_locationInfoNode"];
            var locationInfo = locationInfoNode["_locationInfo" + gameData.players.length];
            if (locationInfo) {
                var peopleNode = self.getPeopleNode(locationInfo, k);
                peopleNode["locationClose"].setString((!loccation) ? '未开启定位' : playerInfo['locUpdTimeStr']);
            }
        }
    },
    /**
     * 获取人物头像节点
     * @param locationInfo
     * @param index
     * @return {*}
     */
    getPeopleNode: function (locationInfo, index) {
        var self = this;
        var peopleNode = locationInfo["people" + index];
        if (!peopleNode) {
            peopleNode = duplicateNode(self._nodeList["_people0"]);
            locationInfo["people" + index] = peopleNode;
            locationInfo.addChild(peopleNode);
            peopleNode.setPosition(locationInfo["pos" + index].getPosition());
        }
        return peopleNode;
    },
    /**
     * 更新定位
     */
    updataJuli: function () {
        var self = this;
        var locationInfoNode = self._nodeList["_locationInfoNode"];
        var children = locationInfoNode.getChildren();
        for (var i = 0; i < children.length; i++) {
            children[i].setVisible(false);
        }
        var locationInfo = locationInfoNode["_locationInfo" + gameData.players.length];
        if (!locationInfo) {
            return;
        }
        locationInfo.setVisible(true);

        if (self.isMain) {
            self._nodeList["_btn_back"].setVisible(true);
        } else {
            if (gameData.anticheating) {
                self._nodeList["_btn_back"].setVisible(self.room.getRoomState() != 1);
                self._nodeList["_btn_tongyi"].setVisible(self.room.getRoomState() == 1);
                self._nodeList["_btn_jujue"].setVisible(self.room.getRoomState() == 1);
            } else {
                self.isUpdate = false;
                self._nodeList["_btn_back"].setVisible(true);
                self._nodeList["_btn_tongyi"].setVisible(false);
                self._nodeList["_btn_jujue"].setVisible(false);
            }
        }
        //var arr = _.keys(self.position2playerArrIdx) || [];
        for (var k = 0; k < gameData.players.length; k++) {
            (function (k) {
                var peopleNode = self.getPeopleNode(locationInfo, k);
                peopleNode.getChildByName('ditanceClose').setVisible(false);

                var playerInfo = gameData.players[k];
                if (playerInfo) {
                    var loccation = playerInfo['loc'] || playerInfo['Location'];
                    ;
                    var headimgurl = playerInfo['headimgurl'] || playerInfo['HeadIMGURL'];
                    loadCircleConorToSprite(headimgurl, peopleNode['head']);
                    peopleNode.getChildByName('locationClose').setString((!loccation) ? '未开启定位' : playerInfo['locUpdTimeStr']);
                }
                //相互距离
                for (var j = k + 1; j < gameData.players.length; j++) {
                    (function (j) {
                        var playerInfo2 = gameData.players[j];
                        var line = locationInfo.getChildByName('lineNode').getChildByName('line_' + k + '_' + j);
                        var juli = line.getChildByName('juliSp').getChildByName('juli');
                        var loccation2 = playerInfo2['loc'] || playerInfo2['Location'];
                        if (playerInfo2 && playerInfo && loccation2 && loccation && line) {
                            var templocation1 = loccation.split(',');
                            var other1Location_1 = templocation1[1];
                            var other1Location_2 = templocation1[0]

                            var templocation2 = loccation2.split(',');
                            var other2Location_1 = templocation2[1];
                            var other2Location_2 = templocation2[0];
                            //距离
                            var distance = LocationUtil.getFlatternDistance(other1Location_1, other1Location_2, other2Location_1, other2Location_2);
                            if (distance >= 1000) {
                                juli.setString((distance / 1000).toFixed(2) + 'km');
                            }
                            else {
                                juli.setString(distance + 'm');
                            }
                            juli.setVisible(true);
                            if (distance > gameData.distance) {
                                line.setColor(cc.color("#008000"));
                                juli.setColor(cc.color("#008000"));
                            } else {
                                line.setColor(cc.color("#FF0000"));
                                juli.setColor(cc.color("#FF0000"));
                            }
                        } else if (line) {
                            line.setColor(cc.color("#FFFFFF"));
                            juli.setString("");
                        }
                    })(j)
                }
            })(k)
        }
        self.updataFangzuobi();
    },
    /**
     * 更新防作弊
     */
    updataFangzuobi: function () {
        var self = this;
        //防作弊
        var listView = self._nodeList["_fangzuobi_bg"]["ListView"];
        for (var i = 0; i < gameData.players.length; i++) {
            var player = gameData.players[i];
            var item = listView['panel' + i];
            if (!item) {
                item = duplicateNode(self._nodeList["_fangzuobi_bg"]["panel0"]);
                item.setName('panel' + i);
                listView['panel' + i] = item;
                listView.pushBackCustomItem(item);
            }
            var head = item.getChildByName('head');
            var locCN = player['locCN'] || player['locationCN'];
            var headimgurl = player['headimgurl'] || player['HeadIMGURL'];
            var nickname = player['NickName'] || player['nickname'];
            var ip = player['ip'] || player['IP'];

            loadHeadImage(headimgurl, head, cc.sys.isNative);
            // item.getChildByName('bg_cell').setVisible(i%2==0);
            item.getChildByName('lb_name').setString(ellipsisStr(decodeURIComponent(nickname || ''), 5));
            item.getChildByName('lb_ip').setString(ip || '');
            item.getChildByName('address').setString(ellipsisStr(decodeURIComponent(locCN || ''), 10));
        }
    },
    /**
     * 刷新按钮
     * @param dir
     */
    refreshTabBtn: function (dir) {
        var self = this;
        // self._nodeList["_log1"].setVisible(dir == 0);
        // self._nodeList["_log2"].setVisible(dir != 0);
        self._nodeList["_tabBtnsNode"]["btn_tab0"]["Image_1"].setVisible(dir == 0);
        self._nodeList["_tabBtnsNode"]["btn_tab1"]["Image_1"].setVisible(dir != 0);
        self._nodeList["_juli_bg"].setVisible(dir == 0);
        self._nodeList["_fangzuobi_bg"].setVisible(dir != 0);
    }
});